/*
 *  Auth: PZ
 *  Date: 2010-10-24
 *  Desc: 常用方法的集合
 * */


var Exz = new Object();

/*
 *  把序列化的参数返回Array
 * */
Exz.$A = function (iterable) {
    if (!iterable) {
        return [];
    }
    if (iterable.toArray) {
        return iterable.toArray();
    } else {
        var result = [];
        for (var i=0, length=iterable.length; i<length; ++i) {
            result.push(iterable[i]);
        }
        return result;
    }
}

/*
 *  函数无限参数传递,如：
 *  map = new google.maps.Map(...);
 *  Exz.pcall(map, map.panTo, new google.maps.LatLng(13.2010239, 18.12938129));
 *
 * */
 
Exz.pcall = function (obj, fun) {
    var o = obj ? obj : window;
    var args = Exz.$A(arguments).slice(2);
    return function () {
        fun.apply(o, args);
    };
}

/*
 *  window.setTimeout的重新实现，支持无限参数传递
 * */
Exz.setTimeout = function (obj, fun, delay) {
    if (typeof fun != 'function') {
        return window.setTimeout(fun, delay);
    }
    var args = Exz.$A(arguments).slice(3);
    var rlt = (function () { fun.apply(obj, args); });

    return window.setTimeout(rlt, delay);

}

/*
 *  window.setInterval的重新实现，支持无限参数传递
 * */
Exz.setInterval = function (obj, fun, delay) {
    if (typeof fun != 'function') {
        return window.setInterval(fun, delay);
    }
    var args = Exz.$A(arguments).slice(3);
    var rlt = (function () { fun.apply(obj, args); });

    return window.setInterval(rlt, delay);

}

/*
 *  可顺序添加的onload事件
 *  Exz.addLoadListener(function)
 *  or
 *  Exz.$load(function)
 * */
Exz.addLoadListener = function (fn) {
    if (typeof window.addEventListener != 'undefined') {
        window.addEventListener('load', fn, false);
    } else if (typeof document.addEventListener != 'undefined') {
        document.addEventListener('load', fn, false);
    } else if (typeof window.attackEvent != 'undefined') {
        window.attackEvent('onload', fn);
    } else {
        var oldfn = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = fn;
        } else {
            window.onload = function () {
                oldfn();
                fn();
            };
        }
    }
    
}
Exz.$load = Exz.addLoadListener;

/*
 *  添加事件链
 *  Exz.addLoadListener(event, function)
 * */
Exz.addEventListener = function (eve, fn) {
    var eval_eve = eval(eve);
    var oldfn = eval_eve;
    if (typeof eval_eve != 'function') {
        eval(eve+"=fn");
    } else {
        var tmp_fn = function () {
            oldfn();
            fn();
        };
        eval(eve+"=tmp_fn");
    }
    
}

/*
 *  可以使函数在onLoad之前并满足dom需求后执行
 *  Exz.addDomFunction(function)
 *  or
 *  Exz.$dom(function)
 *
 *
 *  Exz.$dom(function () { alert('Hello world'); });
 *  Exz.$dom(function, { "elementId": "id" });
 *  Exz.$dom(function, { "p": "tag" });
 * */
Exz.addDomFunction = function (fn, dependencies) {
    var counter = 0, collections = {};
    var timer = setInterval(function () {
            var ready = false;
            counter++;

            if (typeof document.getElementByTagName != 'undefined'
                && (document.getElementByTagName('body')[0] ||
                    document.body))
            {
                ready = true;

                if (typeof dependencies == 'object') {
                    for (var i in dependencies) {
                        if (dependencies[i] == 'id' && 
                            !document.getElementById(i)) 
                        {
                            ready = false;
                            break;
                        } else if (dependencies[i] == 'tag') {
                            var len = document.getElementByTagName(i).length;
                            if (typeof collections[i] == 'undefined' ||
                                collections[i] != len || len < 1) 
                            {
                                collections[i] = len;
                                ready = false;
                                break;
                                
                            }
                        }
                    }
                }
                if (ready) {
                    clearInterval(timer);
                    fn();
                }
            }

            if (counter >= 40) {
                clearInterval(timer);
            }
        
        }, 250);
}
Exz.$dom = Exz.addDomFunction;


/*
 *  获取滚动的坐标
 * */
Exz.getScrollingPosition = function () {
    var position = [0, 0];
    if (typeof window.pageYOffset != 'undefined') {
        position = [
            window.pageXOffset,
            window.pageYOffset
        ];
    } else if (typeof document.documentElement.scrollTop != 'undefined' 
        && document.documentElement.scrollTop > 0)
    {
        position = [
            document.documentElement.scrollLeft,
            document.documentElement.scrollTop
        ];
    } else if (typeof document.body.scrollTop != "undefined") {
        position = [
            document.body.scrollLeft,
            document.body.scrollTop
        ];
    
    }

    return position;
}

/*
 *  获取视区的大小（窗口内可用的空间）
 * */
Exz.getViewportSize = function () {
    var size = [0, 0];
    if (typeof window.innerWidth != 'undefined') {
        size = [
            window.innerWidth,
            window.innerHeight
        ];
    } else if (typeof document.documentElement != 'undefined'
        && typeof document.documentElement.clientWidth != 'undefined'
        && document.documentElement.clientWidth != 0)
    {
        size = [
            document.documentElement.clientWidth,
            document.documentElement.clientHeight
        ];
    } else {
        size = [
            document.getElementsByTagName('body')[0].clientWidth,
            document.getElementsByTagName('body')[0].clientHeight
        ];
    }

    return size;
}

/*
 * 当前页面跳转
 * */
Exz.redirect = function (url) {
    window.location.href = url;
}

/*
 *  如果是iframe中则让父页面成为本页面
 * */
Exz.ensureNotInFrame = function (url) {
    if(self != top) {   
        parent.location.href = url;
    }

}    

/*
 * 设置网站为首页
 * */
Exz.setHomePage = function (self, url) {
    self.style.behavior = 'url(#default#homepage)';
    self.setHomePage(url);
}

/*
 * 添加到收藏夹
 * */
Exz.addFavoriate = function (url, title, errmsg) {
    if (!errmsg) {
        errmsg ="Some errors occurred when add favoriate.";
    }
    try {
        window.external.addFavorite(url, title);
    } catch (e) {
        try {
            window.sidebar.addPanel(title, url, "");
        } catch (e) {
            alert(errmsg);
        }
    
    }
}

/*
 * 复制内容到剪切板
 * */
Exz.copy = function (meintext) {
    if (window.clipboardData) {
        window.clipboardData.setData("Text", meintext);
    } else {
        var flashcopier = 'flashcopier';
        if(!document.getElementById(flashcopier)) {
            var divholder = document.createElement('div');
            divholder.id = flashcopier;
            document.body.appendChild(divholder);
        }
        document.getElementById(flashcopier).innerHTML = '';
        var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(meintext)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
        document.getElementById(flashcopier).innerHTML = divinfo;
    }
    return true;

}

/*
 * 打印和打印预览两个方法
 * */
Exz.printSetup = function () {
    web.execwb(8,1); // 打印页面设置 
}
Exz.printPreview = function () {
    web.execwb(7,1); //打印页面预览
}

/*
 *  可指定小数位的floor
 * */
Exz.floor = function (num, reserve) {
    if (!reserve) reserve = 0;
    var carry = Math.pow(10, reserve);
    return Math.floor(num * carry) / carry;

}

/*
 *  可指定小数位的ceil
 * */
Exz.ceil = function (num, reserve) {
    if (!reserve) reserve = 0;
    var carry = Math.pow(10, reserve);
    return Math.ceil(num * carry) / carry;
}



//////////////////////////////////////////////
//
//  String functions
//  
String.prototype.Trim = function() 
{ 
return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 
 
String.prototype.LTrim = function() 
{ 
return this.replace(/(^\s*)/g, ""); 
} 
 
String.prototype.RTrim = function() 
{ 
return this.replace(/(\s*$)/g, ""); 
} 

String.prototype.Ellipsis = function (sub_length) {
    var temp1 = this.replace(/[^\x00-\xff]/g,"**");//精髓  
    var temp2 = temp1.substring(0,sub_length);  
    //找出有多少个*  
    var x_length = temp2.split("\*").length - 1 ;  
    var hanzi_num = x_length /2 ;  
    sub_length = sub_length - hanzi_num ;//实际需要sub的长度是总长度-汉字长度  
    var res = this.substring(0,sub_length);  
    if(sub_length < this.length ){  
        var end  =res+"..." ;  
    }else{   
        var end  = res ;  
    }  
    return end ;  
}


